package com.wjbgn.electronicnian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.NianDumplingsDO;
import com.wjbgn.electronicnian.dto.NianDumplingsDTO;
import com.wjbgn.electronicnian.service.NianDumplingsService;
import com.wjbgn.electronicnian.convert.NianDumplingsDoConvert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 
 * Create Date: 2022-01-20T11:06:12.872423800
 * @author weirx
 * @version 1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/niandumplings")
@AllArgsConstructor
public class NianDumplingsController {

    /**
     * NianDumplingsService
     */
    private NianDumplingsService nianDumplingsService;

    /**
     * 分页列表
     * @param nianDumplingsDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/pageList")
    public Result pageList(NianDumplingsDTO nianDumplingsDTO) {
        QueryWrapper<NianDumplingsDO> queryWrapper = new QueryWrapper<>(NianDumplingsDoConvert.dtoToDo(nianDumplingsDTO));
        Page<NianDumplingsDO> page = new Page<>(nianDumplingsDTO.getPage(), nianDumplingsDTO.getLimit());
        Page<NianDumplingsDO> pageList = nianDumplingsService.page(page, queryWrapper);
        return Result.success(NianDumplingsDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     * @param nianDumplingsDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @GetMapping("/list")
    public Result list(NianDumplingsDTO nianDumplingsDTO) {
        QueryWrapper<NianDumplingsDO> queryWrapper = new QueryWrapper<>(NianDumplingsDoConvert.dtoToDo(nianDumplingsDTO));
        List<NianDumplingsDO> nianDumplingsList = nianDumplingsService.list(queryWrapper);
        return Result.success(NianDumplingsDoConvert.listConvert(nianDumplingsList));
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public Result info(@RequestParam("id")Long id) {
            NianDumplingsDO nianDumplings = nianDumplingsService.getById(id);
        return Result.success(null == nianDumplings ? null : NianDumplingsDoConvert.doToDto(nianDumplings));
    }

    /**
     * 新增
     * @param nianDumplingsDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody NianDumplingsDTO nianDumplingsDTO) {
        boolean flag = nianDumplingsService.save(NianDumplingsDoConvert.dtoToDo(nianDumplingsDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 更新
     * @param nianDumplingsDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/updateById")
    public Result update(@RequestBody NianDumplingsDTO nianDumplingsDTO) {
        boolean flag = nianDumplingsService.updateById(NianDumplingsDoConvert.dtoToDo(nianDumplingsDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }


    /**
     * 吃饺子
     * @param nianDumplingsDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/eatDumplings")
    public Result eatDumplings(@RequestBody NianDumplingsDTO nianDumplingsDTO) {
        return nianDumplingsService.eatDumplings(NianDumplingsDoConvert.dtoToDo(nianDumplingsDTO));
    }
}